﻿#region Imports

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using bGallery.Core.Data;
using bGallery.Core.Domain;
using Spring.DataBinding;
using Page=bGallery.Core.Common.Page;
using bGallery.DataSource;

#endregion

namespace bGallery.Web
{
	///<summary>
	/// Gallery browsing page
	///</summary>
	public partial class Gallery : Page
	{
		#region Private fields
		
		private IAlbumDao albumDao;
		private ICategoryDao categoryDao;
		private Category selectedCategory;
		private Album selectedAlbum;
		private ICategoryTreeDataSource categoryTreeDataSource;
		
		#endregion

		#region Public properties

		/// <summary>
		/// Sets album DAO
		/// </summary>
		public IAlbumDao AlbumDao
		{
			set { albumDao = value; }
		}

		/// <summary>
		/// Sets cateogory DAO
		/// </summary>
		public ICategoryDao CategoryDao
		{
			set { categoryDao = value; }
		}

        /// <summary>
        /// Gets currently selected album
        /// </summary>
        public Album SelectedAlbum
        {
            get { return selectedAlbum; }
        }

        /// <summary>
        /// Gets currently selected category
        /// </summary>
        public Category SelectedCategory
        {
            get { return selectedCategory; }
        }

		/// <summary>
		/// Sets data source for category
		/// </summary>
		public ICategoryTreeDataSource CategoryTreeDataSource
		{
			set { categoryTreeDataSource = value; }
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Initializes page on load
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
			InitSelectedCategory();
			InitAlbums();
		}

        /// <summary>
        /// Handles category change event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ShowCategory(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((TreeView)sender).SelectedValue);
            selectedCategory = categoryDao.FindById(id);

            SetResult("Gallery");
        }

		#endregion

		#region Model Management and Data Binding Methods

		protected override void InitializeModel()
		{
			selectedAlbum = new Album();
		}

		protected override void LoadModel(object savedModel)
		{
			selectedAlbum = (Album) savedModel;
		}

		protected override object SaveModel()
		{
			return selectedAlbum;
		}

		protected override void InitializeDataBindings()
		{
		}

		#endregion

		#region Private Methods

		/// <summary>
		/// Initializes album list
		/// </summary>
		private void InitAlbums()
		{
			// if category has children
			if (selectedCategory != null && selectedCategory.SubCategories.Count > 0)
			{
				categoryTreeDataSource.RootCategory = selectedCategory;
				categoryTreeDataSource.Update();
				catList.DataSource = categoryTreeDataSource;
				catList.DataBind();
			}

			IList<Core.Domain.Album> albums = new List<Core.Domain.Album>();
			if (selectedCategory != null && selectedCategory.Id > 0)
				albums = albumDao.FindAllByCategory(selectedCategory, SignedInUser);

			// remove empty albums
			IList<Core.Domain.Album> filteredAlbums = new List<Core.Domain.Album>();
			foreach (Core.Domain.Album album in albums)
			{
				if (album.Photos.Count > 0)
					filteredAlbums.Add(album);
			}

			repAlbums.DataSource = filteredAlbums;
			repAlbums.DataBind();
		}

		/// <summary>
		/// Initializes selected catogory
		/// </summary>
		private void InitSelectedCategory()
		{
            if (Request.Params["category"] != null)
            {
                selectedCategory = categoryDao.FindByAbbreviation(Request.Params["category"]);
                lblHeader.Text = selectedCategory.Name;
            }
            else
            {
                lblHeader.Text = GetMessage("defaultHeader");
            }
		}

		#endregion
	}
}
